package com.bbx.taxi.client.Activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import butterknife.InjectView;

import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Activity.Base.BaseBBXActivity;
import com.bbx.taxi.client.Adapter.Page.MyPagerAdapter;
import com.bbx.taxi.client.Adapter.Plane.FlightDetaileAdapter;
import com.bbx.taxi.client.Adapter.Plane.FlightNumberAdapter;
import com.bbx.taxi.client.Bean.Attribute.MainAttribute;
import com.bbx.taxi.client.widget.pop.DatePicker;
import com.bbx.taxi.client.widget.pop.DatePicker.OnConfirmListener;
import com.bbx.taxi.client.widget.pop.DatePicker.OnPoPViewStateListener;
import com.bbx.taxi.client.widget.MyViewPager;
import com.example.mydatepicker.wheelview.WheelView;

/**
 * 接送机(目前接机跟送机返回的数据一模一样，没有区分接机还是送机，等接口来了再根据来区分)
 */
public class PlaneActivity extends BaseBBXActivity implements OnPageChangeListener,OnClickListener,OnPoPViewStateListener{
	@InjectView(R.id.viewpager)MyViewPager mViewPager;
	@InjectView(R.id.image_bar)ImageView image_bar;
	@InjectView(R.id.tv_jieji)TextView tv_jieji;
	@InjectView(R.id.tv_songji)TextView tv_songji;
	@InjectView(R.id.pop_background)View pop_background;
	private RelativeLayout layout_time_jieji;
	private RelativeLayout layout_time_songji;
	private RelativeLayout layout_flight_jieji;
	private RelativeLayout layout_flight_songji;
	private ListView lv_detaile;
	private ListView lv_number;
	private FlightDetaileAdapter mFlightDetaileAdapter;
	private FlightNumberAdapter mFlightNumberAdapter;
	private TextView tv_time_jieji;
	private TextView tv_time_songji;
	private PopupWindow popView_number;
	private PopupWindow popView_detailed;
	private int bmp_width = 0;// 图片大小
	private int bmp_height = 0;// 图片大小
	private List<Map<String,String>> list_detailed;
	private List<String> list_number;
	/**起飞时间*/
	private String plane_time;
	/**航班号*/
	private String plane_numble;
	/**地址*/
	private String plane_end_address;
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_plane);
		super.onCreate(savedInstanceState);
	
	}
	@Override
	protected void initView() {
		setTitle("接送机");
		initPage();
		onInitPopViewNumber();
	}

	@Override
	protected void initData() {
				
	}
	private void initPage() {
		bmp_width = BitmapFactory
				.decodeResource(getResources(), R.drawable.bar).getWidth();// 获取图片宽度
		bmp_height= BitmapFactory
				.decodeResource(getResources(), R.drawable.bar).getHeight();// 获取图片高度度
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		int screenW = dm.widthPixels;// 获取分辨率宽度
		LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) image_bar.getLayoutParams(); //取控件textView当前的布局参数  
		linearParams.height = bmp_height;
		  
		linearParams.width = (screenW/2);
		image_bar.setLayoutParams(linearParams);

		// **********************************
		// 加载ViewPage试图
		ArrayList<View> list = new ArrayList<View>();
		LayoutInflater mInflater = getLayoutInflater();
		View v_jieji = mInflater.inflate(R.layout.activity_plane_content,
				null);// 加载布局
		View v_songji = mInflater.inflate(R.layout.activity_plane_content,
				null);// 加载布局
		// View v3=mInflater.inflate(R.layout.lay3,null);//加载布局
		// 将布局加载到泛型集合
		list.add(v_jieji);
		list.add(v_songji);
		// list.add(v3);
		mViewPager.setAdapter(new MyPagerAdapter(list));// 绑定数据
		mViewPager.setCurrentItem(0);// 设置默认子试图
		mViewPager.setOnPageChangeListener(this);
		layout_time_jieji=(RelativeLayout)v_jieji.findViewById(R.id.layout_time);
		layout_flight_jieji=(RelativeLayout)v_jieji.findViewById(R.id.layout_flight);
		tv_time_jieji=(TextView)v_jieji.findViewById(R.id.tv_time);
		layout_time_songji=(RelativeLayout)v_songji.findViewById(R.id.layout_time);
		layout_flight_songji=(RelativeLayout)v_songji.findViewById(R.id.layout_flight);
		tv_time_songji=(TextView)v_songji.findViewById(R.id.tv_time);

		layout_time_jieji.setOnClickListener(this);
		layout_flight_jieji.setOnClickListener(this);
		layout_time_songji.setOnClickListener(this);
		layout_flight_songji.setOnClickListener(this);
		pop_background.setOnClickListener(this);
		DatePicker.setOnPoPViewStateListener(this);
	}
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onPageSelected(int arg0) {
		Animation animation = null;
		switch (arg0) {
		case 0:
			animation=AnimationUtils.loadAnimation(this,R.anim.viewpagebar_right);
			tv_jieji.setTextColor(getResources()
					.getColor(R.color.light_blue));
			tv_songji.setTextColor(getResources().getColor(R.color.black));
			break;
		case 1:
			animation=AnimationUtils.loadAnimation(this,R.anim.viewpagebar_left);
			tv_jieji.setTextColor(getResources().getColor(R.color.black));
			tv_songji.setTextColor(getResources()
					.getColor(R.color.light_blue));
			break;

		}
		animation.setFillAfter(true);// True:图片停在动画结束位置
		animation.setDuration(300);
		image_bar.startAnimation(animation);
	}
	@Override
	public void onClick(View v) {
		if(v==layout_time_jieji){//接机——日期
			DatePicker.getInstance(this).onShow(v, 3);
			DatePicker.getInstance(this).setOnConfirmListener(new OnConfirmListener() {
				
				@Override
				public void onConfirm(List<String> list,WheelView day) {
					tv_time_jieji.setText(DatePicker.getInstance(PlaneActivity.this).onTimeFormat(list.get(day.getCurrentItem())));
					plane_time=tv_time_jieji.getText().toString();
				}
			});
		}
		else if(v==layout_flight_jieji){//接机——航班
			if(plane_time==null||plane_time.equals("")){
				Toast.makeText(this, "请填写时间", Toast.LENGTH_SHORT).show();
				return ;
			}
			onShowPopViewNumber(v);
		}
		else if(v==layout_time_songji){//送机——日期
			DatePicker.getInstance(this).onShow(v, 3);
			DatePicker.getInstance(this).setOnConfirmListener(new OnConfirmListener() {
				
				@Override
				public void onConfirm(List<String> list,WheelView day) {
					tv_time_songji.setText(DatePicker.getInstance(PlaneActivity.this).onTimeFormat(list.get(day.getCurrentItem())));
					plane_time=tv_time_songji.getText().toString();
				}
			});
		}
		else if(v==layout_time_songji){//送机——日期
			if(plane_time==null||plane_time.equals("")){
				Toast.makeText(this, "请填写时间", Toast.LENGTH_SHORT).show();
				return ;
			}
			onShowPopViewNumber(v);
		}
		else{
			super.onClick(v);
		}
	}
    public void onInitPopViewDetailed(){
    	LayoutInflater inflater = LayoutInflater.from(this);
		View popview = inflater.inflate(R.layout.pop_flight, null); 
		popView_detailed = new PopupWindow(popview,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
		popView_detailed.setBackgroundDrawable(new BitmapDrawable());
		popView_detailed.setContentView(popview);	
		popView_detailed.setOutsideTouchable(false); 
		popView_detailed.setFocusable(true);
		popView_detailed.setAnimationStyle(R.style.mypopwindow_anim_style);
		list_detailed=new ArrayList<Map<String,String>>();
		for(int i=0;i<4;i++){
			Map<String, String> map=new HashMap<String, String>();
			map.put("city","福州  至  大连");
			map.put("date","07月24号");
			map.put("time","14:05到达");
			list_detailed.add(map);
		}
		mFlightDetaileAdapter=new FlightDetaileAdapter(this, list_detailed);

		lv_detaile=(ListView)popview.findViewById(R.id.list_detaile);
		lv_detaile.setAdapter(mFlightDetaileAdapter);
		lv_detaile.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				plane_end_address="厦门高崎机场T4候机楼";
				Bundle bundle=new Bundle();
				bundle.putInt("plane_status",1);
				bundle.putString("plane_time",plane_time);
	    		bundle.putString("plane_numble",plane_numble);
	    		bundle.putString("plane_end_address",plane_end_address);
	    		Intent intent=new Intent();
	    		intent.putExtras(bundle);
	    		setResult(MainAttribute.REQUEST_CODE_PLANE,intent);   
	    		finish();
			}
		});
    }
    public void onInitPopViewNumber(){
    	LayoutInflater inflater = LayoutInflater.from(this);
		View popview = inflater.inflate(R.layout.pop_flight_number, null); 
		popView_number = new PopupWindow(popview,LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
		popView_number.setBackgroundDrawable(new BitmapDrawable());
		popView_number.setContentView(popview);	
		popView_number.setOutsideTouchable(false); 
		popView_number.setFocusable(true);
		popView_number.setAnimationStyle(R.style.mypopwindow_anim_style);
		list_number=new ArrayList<String>();
		for(int i=0;i<4;i++){
			list_number.add("CA0101");
		}
		mFlightNumberAdapter=new FlightNumberAdapter(this, list_number);

		lv_number=(ListView)popview.findViewById(R.id.list_number);
		lv_number.setAdapter(mFlightNumberAdapter);
		lv_number.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				popView_number.dismiss();
				onInitPopViewDetailed();
				onShowPopViewDetailed(layout_flight_jieji);
				plane_numble=list_number.get(position);
			}
			
		});
		
    }
    public void onShowPopViewDetailed(View v){
    	popView_detailed.showAsDropDown(v);
    	mFlightDetaileAdapter.notifyDataSetChanged();
    }
    public void onShowPopViewNumber(View v){
    	popView_number.showAsDropDown(v);
    	mFlightNumberAdapter.notifyDataSetChanged();
    }
	@Override
	public void onPopViewState(boolean isOpen) {
		if(isOpen){
			pop_background.setVisibility(View.VISIBLE);
		}
		else{
			pop_background.setVisibility(View.GONE);
		}
	}
	@Override
	protected void onDestroy() {
		DatePicker.removeOnPoPViewStateListener(this);
		super.onDestroy();
	}
}
